package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {

    @Reference
    private ContractService contractService;
    @Reference
    private ContractProductService contractProductService;
    @Reference
    private FactoryService factoryService;
    @Reference
    private ExtCproductService extCproductService;

    @RequestMapping(value = "/list",name = "展示购销合同货物下附件列表数据")
    public String findAll(String contractId,String contractProductId,@RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "10") Integer pageSize){

//        1、所有生产附件的工厂
//        2、这个货物下的所有附件
//        3、隐藏域 合同ID和货物id

//        查询购销合同附件 应该有什么条件 contractProductId
        ExtCproductExample example = new ExtCproductExample();
        example.createCriteria().andContractProductIdEqualTo(contractProductId);
        PageInfo<ExtCproduct> pageInfo = extCproductService.findAll(example,page,pageSize);
//        1、当前货物的所有附件数据
        request.setAttribute("page",pageInfo); //这个 “page” 不能写错，因为页面就是取的是page中的值

        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
//        2、用来生产附件的工厂数据
        request.setAttribute("factoryList",factoryList);

//        3、购销合同的id和货物id
        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);

        return "cargo/extc/extc-list";
    }


   /* @RequestMapping(value = "/toAdd",name = "进入到添加购销合同页面")
    public String toAdd(){
        return "cargo/contract/contract-add";
    }*/

   @Autowired
    private FileUploadUtil fileUploadUtil;

    @RequestMapping(value = "/edit",name = "保存购销合同货物附件方法")
    public String edit(ExtCproduct extCproduct, MultipartFile productPhoto ){
        String imageUrl = null;
        try {
            imageUrl = fileUploadUtil.upload(productPhoto);
        } catch (Exception e) {
            imageUrl=null;
            e.printStackTrace();
        }
        extCproduct.setProductImage(imageUrl);

        if(StringUtils.isEmpty(extCproduct.getId())){
            extCproduct.setId(UUID.randomUUID().toString());
            extCproduct.setCompanyId(getCompanyId());
            extCproduct.setCompanyName(getCompanyName());
            extCproduct.setCreateTime(new Date());
            extCproductService.save(extCproduct);
        }else{
            extCproductService.update(extCproduct);
        }
        return "redirect:/cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId();
    }

   @RequestMapping(value = "/toUpdate",name = "进入到修改购销合同货物附件页面")
    public String toUpdate(String id,String contractId,String contractProductId){
//        需要回显的数据有：
//        1、当前的附件数据  2、用来生产附件的工厂数据  3、购销合同的id和货物id

        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct",extCproduct);


        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
//        2、用来生产货物的工厂数据
        request.setAttribute("factoryList",factoryList);

       //        3、购销合同的id和货物id
       request.setAttribute("contractId",contractId);
       request.setAttribute("contractProductId",contractProductId);
        return "cargo/extc/extc-update";
    }


    @RequestMapping(value = "/delete",name = "删除购销合同货物附件方法")
    public String delete(String id,String contractId,String contractProductId){
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+contractProductId;
    }
}
